<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="供應商編碼" prop="supplierCode">
        <el-input
          v-model="queryParams.supplierCode"
          placeholder="請輸入供應商編碼"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供應商名稱" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="請輸入供應商名稱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="聯繫人" prop="contacts">
        <el-input
          v-model="queryParams.contacts"
          placeholder="請輸入聯繫人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手機號" prop="telphone">
        <el-input
          v-model="queryParams.telphone"
          placeholder="請輸入手機號"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="郵箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="請輸入郵箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供應商分類" prop="supplierType">
        <el-select
          v-model="queryParams.supplierType"
          placeholder="請選擇供應商分類"
          clearable
        >
          <el-option
            v-for="dict in dict.type.supplier_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="服務情況" prop="serviceSituation">
        <el-select
          v-model="queryParams.serviceSituation"
          placeholder="請選擇服務情況"
          clearable
        >
          <el-option
            v-for="dict in dict.type.supplier_service_situation"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="行業" prop="industry">
        <el-select
          v-model="queryParams.industry"
          placeholder="請選擇所屬行業"
          clearable
        >
          <el-option
            v-for="dict in dict.type.industry_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="時間範圍" prop="time">
        <el-date-picker
          v-model="timeData"
          type="datetimerange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['biz:supplier:add']"
          >新增</el-button
        >
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['biz:supplier:export']"
          >導出報表</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport_list"
          v-hasPermi="['biz:supplier:export']"
          >導出列表</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['biz:supplier:edit']"
          >修改</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['biz:supplier:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['biz:supplier:export']"
          >導出</el-button
        >
      </el-col> -->

      <el-popover
        style="float: right; margin-left: 5px"
        placement="bottom-start"
        width="100"
        trigger="click"
      >
        <div>
          <el-checkbox-group
            v-model="checked_header"
            @change="Checked_header_list"
          >
            <el-checkbox
              :disabled="item.disabled"
              v-for="item in header_list"
              :label="item.prop"
              :key="item.prop"
              >{{ item.name }}</el-checkbox
            >
          </el-checkbox-group>
        </div>
        <el-button slot="reference" size="mini" circle icon="el-icon-setting" />
      </el-popover>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
      <!-- <el-button icon="el-icon-setting" size="mini" @click="resetQuery"
          >重置</el-button
        > -->
    </el-row>

    <el-table
      v-loading="loading"
      :data="supplierList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column
        show-overflow-tooltip
        v-if="checked_header.includes('supplierCode')"
        width="180"
        label="供應商編碼"
        align="center"
        prop="supplierCode"
      />
      <el-table-column
        show-overflow-tooltip
        v-if="checked_header.includes('supplierName')"
        width="120"
        label="供應商名稱"
        align="center"
        prop="supplierName"
      >
        <template slot-scope="scope">
          <span
            style="color: #1890ff; cursor: pointer"
            @click="Go_cost(scope.row.supplierName)"
            >{{ scope.row.supplierName }}</span
          >
        </template>
      </el-table-column>

      <el-table-column
        show-overflow-tooltip
        v-if="checked_header.includes('contacts')"
        label="聯繫人"
        align="center"
        prop="contacts"
      />
      <el-table-column
        show-overflow-tooltip
        label="職務"
        align="center"
        v-if="checked_header.includes('contactsPost')"
        prop="contactsPost"
      />

      <el-table-column
        show-overflow-tooltip
        label="手機號"
        v-if="checked_header.includes('telphone')"
        align="center"
        prop="telphone"
      />

      <el-table-column
        show-overflow-tooltip
        label="郵箱"
        v-if="checked_header.includes('email')"
        align="center"
        prop="email"
      />
      <el-table-column
        show-overflow-tooltip
        label="傳真"
        v-if="checked_header.includes('facsimile')"
        align="center"
        prop="facsimile"
      />
      <el-table-column
        show-overflow-tooltip
        label="公司地址"
        v-if="checked_header.includes('address')"
        align="center"
        prop="address"
      />
      <el-table-column
        show-overflow-tooltip
        label="稅率"
        v-if="checked_header.includes('taxRate')"
        align="center"
        prop="taxRate"
      />
      <el-table-column
        show-overflow-tooltip
        width="100"
        v-if="checked_header.includes('supplierType')"
        label="供應商分類"
        align="center"
        prop="supplierType"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.supplier_type"
            :value="scope.row.supplierType"
          />
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        v-if="checked_header.includes('serviceSituation')"
        label="服務情況"
        align="center"
        prop="serviceSituation"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.supplier_service_situation"
            :value="scope.row.serviceSituation"
          />
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="行業"
        v-if="checked_header.includes('industryName')"
        align="center"
        prop="industryName"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.industry_type"
            :value="scope.row.industry"
          />
        </template>
      </el-table-column>

      <el-table-column
        label="入駐時間"
        v-if="checked_header.includes('inTime')"
        align="center"
        prop="inTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.inTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        width="100"
        v-if="checked_header.includes('supplierStatus')"
        label="供應商狀態"
        align="center"
        prop="supplierStatus"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.supplier_status"
            :value="scope.row.supplierStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="160"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['biz:supplier:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['biz:supplier:remove']"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            >詳情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改供應商对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="供應商編碼" prop="supplierCode">
          <el-input
            v-model="form.supplierCode"
            placeholder="請輸入供應商編碼"
          />
        </el-form-item>
        <el-form-item label="供應商名稱" prop="supplierName">
          <el-input
            v-model="form.supplierName"
            placeholder="請輸入供應商名稱"
          />
        </el-form-item>
        <el-form-item label="聯繫人" prop="contacts">
          <el-input v-model="form.contacts" placeholder="請輸入聯繫人" />
        </el-form-item>
        <el-form-item label="職務" prop="contactsPost">
          <el-input v-model="form.contactsPost" placeholder="請輸入職務" />
        </el-form-item>
        <el-form-item label="手機號" prop="telphone">
          <el-input v-model="form.telphone" placeholder="請輸入手機號" />
        </el-form-item>
        <el-form-item label="郵箱" prop="email">
          <el-input v-model="form.email" placeholder="請輸入郵箱" />
        </el-form-item>
        <el-form-item label="傳真" prop="facsimile">
          <el-input v-model="form.facsimile" placeholder="請輸入傳真" />
        </el-form-item>
        <el-form-item label="公司地址" prop="address">
          <el-input v-model="form.address" placeholder="請輸入公司地址" />
        </el-form-item>
        <el-form-item label="稅率" prop="taxRate">
          <el-input
            v-model="form.taxRate"
            clearable
            placeholder="請輸入稅率"
            oninput="value=value.replace(/^0[0-9]|^[2-9]|^1[0-9]|^1\.|[^\d.]/g,'')"
          />
          <!-- <el-input v-model="form.taxRate"  placeholder="請輸入稅率" /> -->
        </el-form-item>
        <el-form-item label="供應商分類" prop="supplierTypeName">
          <el-input
            v-model="form.supplierTypeName"
            placeholder="請輸入供應商分類"
          />
        </el-form-item>
        <el-form-item label="服務情況KEY" prop="serviceSituation">
          <el-input
            v-model="form.serviceSituation"
            placeholder="請輸入服務情況KEY"
          />
        </el-form-item>
        <el-form-item label="服務情況" prop="serviceSituationName">
          <el-input
            v-model="form.serviceSituationName"
            placeholder="請輸入服務情況"
          />
        </el-form-item>
        <el-form-item label="行業KEY" prop="industry">
          <el-input v-model="form.industry" placeholder="請輸入行業KEY" />
        </el-form-item>
        <el-form-item label="行業" prop="industryName">
          <el-input v-model="form.industryName" placeholder="請輸入行業" />
        </el-form-item>
        <el-form-item label="入駐時間" prop="inTime">
          <el-date-picker
            clearable
            v-model="form.inTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="請選擇入駐時間"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="刪除標記" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="請輸入刪除標記" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">確 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSupplier,
  getSupplier,
  delSupplier,
  addSupplier,
  updateSupplier,
} from "@/api/biz/supplier";

export default {
  name: "Supplier",
  dicts: [
    "supplier_type",
    "sys_yes_no",
    "sys_normal_disable",
    "tax_nature",
    "supplier_service_situation",
    "industry_type",
    "supplier_status",
  ],
  data() {
    return {
      timeData: [
        this.$moment().subtract(1, "years").format("YYYY-MM-DD"),
        this.$moment().format("YYYY-MM-DD"),
      ],
      // 遮罩层
      loading: true,
      // 选中數組
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 顯示搜索条件
      showSearch: true,
      // 总条數
      total: 0,
      // 供應商表格數据
      supplierList: [],
      // 弹出层標题
      title: "",
      // 是否顯示弹出层
      open: false,
      // 查询參數
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierCode: null,
        supplierName: null,
        contacts: null,
        contactsPost: null,
        telphone: null,
        email: null,
        facsimile: null,
        address: null,
        taxNature: null,
        taxNatureName: null,
        taxRate: null,
        supplierType: null,
        supplierTypeName: null,
        serviceSituation: null,
        serviceSituationName: null,
        industry: null,
        industryName: null,
        inTime: null,
        supplierStatus: null,
        delFlag: null,
        orderByColumn: "update_time",
        isAsc: "desc",
      },
      // 表单參數
      form: {},
      // 表单校验
      rules: {
        supplierCode: [
          { required: true, message: "供應商編碼不能為空", trigger: "blur" },
        ],
      },
      // supplierCode
      // supplierName
      // contacts
      // contactsPost
      // telphone
      header_list: [
        {
          name: "供應商編碼",
          disabled: true,
          prop: "supplierCode",
        },
        {
          name: "供應商名稱",
          disabled: true,
          prop: "supplierName",
        },
        {
          name: "聯繫人",
          disabled: false,
          prop: "contacts",
        },
        {
          name: "職務",
          disabled: false,
          prop: "contactsPost",
        },
        {
          name: "手機號",
          disabled: false,
          prop: "telphone",
        },
        {
          name: "郵箱",
          disabled: false,
          prop: "email",
        },
        {
          name: "傳真",
          disabled: false,
          prop: "facsimile",
        },
        {
          name: "公司地址",
          disabled: false,
          prop: "address",
        },
        {
          name: "稅率",
          disabled: false,
          prop: "taxRate",
        },
        {
          name: "供應商分類",
          disabled: false,
          prop: "supplierType",
        },
        {
          name: "服務情況",
          disabled: false,
          prop: "serviceSituation",
        },
        {
          name: "行業",
          disabled: false,
          prop: "industryName",
        },
        {
          name: "入駐時間",
          disabled: false,
          prop: "inTime",
        },
        {
          name: "供應商狀態",
          disabled: false,
          prop: "supplierStatus",
        },
      ],

      checked_header: [
        "supplierCode",
        "supplierName",
        "contacts",
        // "contactsPost",
        "telphone",
        "email",
        // "facsimile",
        // "address",
        // "taxNature",
        // "taxRate",
        "supplierType",
        "serviceSituation",
        "industryName",
        // "inTime",
        "supplierStatus",
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    Checked_header_list(list) {
      console.log(this.checked_header);
    },
    Go_cost(name) {
      // this.$router.push({ name: 'Cost', params: { supplierName: name }})
      const obj = { path: "/cost/cost", name: "Cost" };
      this.$tab.refreshPage(obj).then(() => {
        // 执行結束的逻辑
        this.$router.push({
          path: "/cost/cost",
          query: {
            supplierName: name,
          },
        });
      });
    },
    /** 查询供應商列表 */
    getList() {
      this.loading = true;
      listSupplier(this.queryParams).then((response) => {
        this.supplierList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        supplierCode: null,
        supplierName: null,
        contacts: null,
        contactsPost: null,
        telphone: null,
        email: null,
        facsimile: null,
        address: null,
        taxNature: null,
        taxNatureName: null,
        taxRate: null,
        supplierType: null,
        supplierTypeName: null,
        serviceSituation: null,
        serviceSituationName: null,
        industry: null,
        industryName: null,
        inTime: null,
        supplierStatus: null,
        createTime: null,
        createBy: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.startDate = this.timeData[0];
      this.queryParams.endDate = this.timeData[1];
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中數据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.$router.push({ path: "/supplier/supplier/add" });
      // this.open = true;
      // this.title = "添加供應商";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getSupplier(id).then((response) => {
        // this.form = response.data;
        this.$router.push({
          path: "/supplier/supplier/edit",
          query: response.data,
        });
        // this.open = true;
        // this.title = "修改供應商";
      });
    },
    handleDetail(row) {
      const id = row.id || this.ids;
      this.$router.push({
        path: "/supplier/supplier/detail",
        query: { id: id },
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateSupplier(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSupplier(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否確認删除供應商編號為"' + ids + '"的數据项？')
        .then(function () {
          return delSupplier(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 導出按钮操作 */
    /** 導出按钮操作 */
    handleExport() {
      this.queryParams.startDate = this.timeData[0];
      this.queryParams.endDate = this.timeData[1];
      // 提交時間
      // startDate
      // endDate
      this.download(
        "biz/supplier/cost/export",
        {
          // startDate:this.timeData[0],
          // endDate:this.timeData[1],
          // startPayDate:this.timeData[0],
          // endPayDate:this.timeData[1],
          ...this.queryParams,
        },
        `供應商_${new Date().getTime()}.xlsx`
      );
    },
    handleExport_list() {
      this.download(
        "biz/supplier/export",
        {
          ...this.queryParams,
        },
        `供應商列表_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
